package com.scenery.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.scenery.Constans;
import com.scenery.util.ClientUtilAndroid;
import com.scenery.util.HttpUtil;
import com.scenery.util.XmlPullUtil;
import com.scenery.vo.City;
import com.scenery.vo.Scenery;
/****
 * 景点操作类
 * */
public class SceneryServices {
	
	
	private String tag=SceneryServices.class.getName();
	
	String ip,port;
	Context con;
	public SceneryServices(String ip,String port,Context con)
	{
		this.ip=ip;
		this.port=port;
		this.con=con;
	}
	
	public SceneryServices(Context con)
	{
		this.con=con;
	}
	/**
	 * 海南城市列表
	 * 
	 * **/
	public  ArrayList<City> getCityListByProvinceId() {
		Log.d(tag, "getCityListByProvinceId");
		ArrayList<City> cityList = null ;
	
		try {
			 cityList=XmlPullUtil.pullCityByAssert(con.getAssets().open("hainan_city.xml"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cityList;
	}
	
	/**
	 * 获取景点列表 
	 * **/
	public ArrayList<Scenery> getSceneryList(String cityId){
		Log.d(tag, "getSceneryList");
		 ArrayList<Scenery> list=new  ArrayList<Scenery> ();
		
		//String json=HttpUtil.httpRequestInfo( Constans.scenery_bycity_URL+cityId);
		ArrayList<NameValuePair> parameters=new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("cityId",cityId));
		String json=	HttpUtil.httpPost(parameters,  "http://"+ip+":"+port+Constans.scenery_bycity_URL);
		Log.d(tag, "getSceneryList json:"+json);
		
		try 
		{
			JSONObject jo=new JSONObject(json);
			
			if(jo.getString("code").equals("100"))
			{
				JSONArray array=jo.getJSONArray("list");
				for(int i=0;i<array.length();i++)
				{
					JSONObject rs=array.getJSONObject(i);
					Scenery bean =new Scenery();
					bean.setSceneryId(rs.getString("sceneryId"));
					Log.d(tag, "getSceneryList json:"+rs.getString("sceneryId"));
					bean.setSceneryName(rs.getString("sceneryName"));
					bean.setGrade(rs.getString("grade"));
					bean.setAddress(rs.getString("address"));
					bean.setCityId(rs.getString("cityId"));
					bean.setProvinceId(rs.getString("provinceId"));
					bean.setIntro(rs.getString("intro"));
					bean.setBuyNotice(rs.getString("buyNotice"));
					bean.setPayMode(rs.getString("payMode"));
					bean.setLon(rs.getString("lon"));
					bean.setLat(rs.getString("lat"));
					list.add(bean);
				}
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d(tag, "list size:"+list.size());
		return list;
		
		
	}
	
}
